$(function(){
	/**
     * 分页初始条件
     */
    var total = 1;//分页总页面数
    var total_count = 1;//分页总记录数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数

	
    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
    	//根据条件查询商品信息
    	$('.search-button').click(function(){
    		render(true,1,pageSize);
            return false;
		});

    }
    
    
    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {
            is_use : $(".search-area #is_use").val(),
            coupon_id:$(".search-area #coupon_id").val()
        };
        return selectInfo;
    }
    
    
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }
    
    
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingUserCoupon',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
            	console.log(result);
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total});//重新计算总页数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;
                    html+='<tr><th class="th1">序号</th><th class="th4">用户昵称</th><th class="th4">优惠券编号</th><th class="th5">领取时间</th><th class="th5">剩余数/使用数</th><th class="th5">所属商城</th><th class="th5">使用时间</th></tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1) * pageSize + i+1;
                        var nick_name = obj.nick_name;
                        var coupon_num = obj.coupon_num;
                        var add_time = obj.add_time;
                        var is_use = obj.is_use;
                        var inventory = obj.inventory ? obj.inventory : '--' ;
                        var coupon_used = obj.coupon_used ? obj.coupon_used : '--' ;
                        var use_time = obj.use_time?obj.use_time:"--";
                   
                        var sale_value = obj.sale_value ? obj.sale_value+'元' : '--' ;
                        var type = obj.type;
                        var type_text = '';
                        if(type == 1){
                        	type_text = '<td>拼团商城</td>';
                        }else if(type == 2){
                        	type_text = '<td>秒杀商城</td>';
                        }else{
                        	type_text = '<td>--</td>';
                        }
                        
                       
                        var id = obj.id;

                        html+='<tr>'
                            +'<td>'+num+'</td>'
                            +'<td>'+ nick_name +'</td>'
                            +'<td>'+coupon_num+'</td>'
                            +'<td>'+add_time+'</td>'
                            +'<td>'+inventory+'/'+coupon_used+'</td>'
                            +type_text
                            +'<td>'+use_time+'</td>'                   
                            +'</tr>';

                    }


                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                    }
                    $("#list-table tbody").html(html);
                }else{
                    $("#myModal .modal-body").html("<p class='text-danger'>"+result.errorInfo+"</p>");
                    $("#myModal").modal('show');
                    //定时器，1.5秒后模态框自动关闭
                    setTimeout(function(){
                        $("#myModal").modal('hide');
                    },1500);
                }

            },
            error:errorResponse
        });
    }

    init();
});